package ch.subgate.spritestudio.widgets;

import ch.subgate.spritestudio.tools.OnAlphaChangedListener;
import ch.subgate.spritestudio.tools.OnColorChangedListener;
import ch.subgate.spritestudio.tools.OnHueChangedListener;
import ch.subgate.spritestudio.tools.OnSaturationChangedListener;
import ch.subgate.spritestudio.tools.OnValueChangedListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class HSVAPresenterView extends View implements
OnAlphaChangedListener,
OnHueChangedListener,
OnSaturationChangedListener,
OnValueChangedListener {
	
	/** Current alpha component value */
	private int mAlpha;
	
	/** Current HSV color value */
	private float[] mHSVColor;
	
	private int mOldColor;
	
	/** Paint object to draw the overlaying color */
	private Paint mColorPaint;
	
	/** Paint object to draw the background pattern */
	private Paint mPatternPaint;
	
	/** Paint object to draw the boundaries */
	private Paint mBoundsPaint;
	
	/** Color change listener */
	private OnColorChangedListener mListener;

	public HSVAPresenterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		//Set the default hsv color
		mHSVColor = new float[3];
		mHSVColor[0] = 1.0f;
		mHSVColor[1] = 1.0f;
		mHSVColor[2] = 1.0f;
		
		//Set the default alpha value
		mAlpha = 255;
		
		mOldColor = 0xff000000;
		
		//Initialize the paint objects
		mColorPaint = new Paint();
		mPatternPaint = new Paint();
		mBoundsPaint = new Paint();
		
		mListener = null;
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		//Draw the background pattern
		for (int i = 0; i * 36 < this.getMeasuredWidth(); i++) {
			for (int j = 0; j * 36 < this.getMeasuredHeight(); j++) {
				if ((i + j) % 2 == 0) {
					mPatternPaint.setColor(0xffaaaaaa);
					canvas.drawRect(
							(float)(i * 36),
							(float)(j * 36),
							(float)((i + 1) * 36),
							(float)((j + 1) * 36),
							mPatternPaint);
				}
				else {
					mPatternPaint.setColor(0xffcccccc);
					canvas.drawRect(
							(float)(i * 36),
							(float)(j * 36),
							(float)((i + 1) * 36),
							(float)((j + 1) * 36),
							mPatternPaint);
				}
			}
		}
		
		//Draw the overlaying color
		mColorPaint.setColor(mOldColor);
		canvas.drawRect(
				0.0f,
				0.0f,
				((float)this.getMeasuredWidth()) / 2.0f,
				(float)this.getMeasuredHeight(),
				mColorPaint);
		mColorPaint.setColor(Color.HSVToColor(mAlpha, mHSVColor));
		canvas.drawRect(
				((float)this.getMeasuredWidth()) / 2.0f,
				0.0f,
				(float)this.getMeasuredWidth(),
				(float)this.getMeasuredHeight(),
				mColorPaint);
		
		//Draw the borders
		mBoundsPaint.setColor(0x66ffffff);
		canvas.drawLine(0.0f, 0.0f, this.getMeasuredWidth(), 0.0f, mBoundsPaint);
		canvas.drawLine(0.0f, 0.0f, 0.0f, this.getMeasuredHeight(), mBoundsPaint);
		mBoundsPaint.setColor(0x66000000);
		canvas.drawLine(0.0f, this.getMeasuredHeight() - 1.0f, this.getMeasuredWidth(), this.getMeasuredHeight() - 1.0f, mBoundsPaint);
		canvas.drawLine(this.getMeasuredWidth() - 1.0f, 0.0f, this.getMeasuredWidth() - 1.0f, this.getMeasuredHeight(), mBoundsPaint);
	}

	@Override
	public void onValueChanged(float value) {
		mHSVColor[2] = value;
		mColorPaint.setColor(Color.HSVToColor(mAlpha, mHSVColor));
		onColorChanged();
		invalidate();
	}

	@Override
	public void onSaturationChanged(float saturation) {
		mHSVColor[1] = saturation;
		mColorPaint.setColor(Color.HSVToColor(mAlpha, mHSVColor));
		onColorChanged();
		invalidate();
	}

	@Override
	public void onHueChanged(float hue) {
		mHSVColor[0] = hue;
		mColorPaint.setColor(Color.HSVToColor(mAlpha, mHSVColor));
		onColorChanged();
		invalidate();
	}

	@Override
	public void onAlphaChanged(int alpha) {
		mAlpha = alpha;
		mColorPaint.setColor(Color.HSVToColor(mAlpha, mHSVColor));
		onColorChanged();
		invalidate();
	}
	
	public void onColorChanged() {
		if (mListener != null) {
			mListener.onColorChanged(Color.HSVToColor(mAlpha, mHSVColor));
		}
	}
	
	public int getColor() {
		return Color.HSVToColor(mAlpha, mHSVColor);
	}
	
	public void setOldColor(int color) {
		mOldColor = color;
		invalidate();
	}
	
	public int getOldColor() {
		return mOldColor;
	}
	
	public void setOnColorChangedListener(OnColorChangedListener listener) {
		mListener = listener;
	}
}
